const momentService = require('../service/moment.service')
const fileService = require('../service/file.service')
const {PICTURE_PATH} = require('../constants/file-path')
const fs = require('fs')
class MomentController {
  async create (ctx, next){
    //1.获取数据（user_id, content）
    // console.log(ctx.user)
    const userId = ctx.user.id
    const {content} = ctx.request.body
    //2.将数据插入数据库中
    const result = await momentService.create(userId, content)
    ctx.body = result
  }
  async detail(ctx, next){
    const {momentId} = ctx.params
    const result = await momentService.getMomentById(momentId)
    ctx.body = result
  }
  async list(ctx, next){
    //1.获取数据（offset/size）
    const {offset, size} = ctx.query
    //2.查询列表
    const result = await momentService.getMomentList(offset,size)
    ctx.body = result
  }
  async update(ctx, next) {
    const {momentId} = ctx.params
    const {content} = ctx.request.body
    const result = await momentService.update(content, momentId)
    ctx.body = result
  }
  async remove (ctx,next) {
    const {momentId} = ctx.params
    const result = await momentService.remove(momentId)
    ctx.body = result
  }
  async addLabels(ctx,next) {
    //1.获取标签和动态id
    const {labels} = ctx
    const {momentId} = ctx.params
    //2.添加所有标签
    for(let label of labels){
      // 2.1判断标签是否和动态有关系
      const isExists = await momentService.hasLabel(momentId, label.id)
      if(!isExists){
        await momentService.addLabel(momentId, label.id)
      }
    }
    ctx.body = '给动态标签成功'
  }
  async fileInfo(ctx, next) {
    let { filename } = ctx.params;
    const fileInfo = await fileService.getFileByFilename(filename);
    const { type } = ctx.query;
    const types = ["small", "middle", "large"];
    if (types.some(item => item === type)) {
      filename = filename + '-' + type;
    }

    ctx.response.set('content-type', fileInfo.mimetype);
    ctx.body = fs.createReadStream(`${PICTURE_PATH}/${filename}`);
  }
}
module.exports = new MomentController()